package com.atguigu.tingshu.album.api;

import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.album.service.VodService;
import com.atguigu.tingshu.common.login.GuiGuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.AlbumTrackListVo;
import com.atguigu.tingshu.vo.album.TrackInfoVo;
import com.atguigu.tingshu.vo.album.TrackListVo;
import com.atguigu.tingshu.vo.album.TrackStatVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@Tag(name = "声音管理")
@RestController
@RequestMapping("api/album")
@SuppressWarnings({"all"})
public class TrackInfoApiController {

    @Autowired
    private TrackInfoService trackInfoService;

    @Autowired
    private VodService vodService;


    /**
     * 音视频文件上传点播平台
     *
     * @param file
     * @return {mediaFileId:"文件唯一标识",mediaUrl:"在线播放地址"}
     */
    @Operation(summary = "音视频文件上传点播平台")
    @PostMapping("/trackInfo/uploadTrack")
    public Result<Map<String, String>> uploadTrack(@RequestParam("file") MultipartFile file) {
        Map<String, String> map = vodService.uploadTrack(file);
        return Result.ok(map);
    }


    /**
     * 该接口登录才可以访问
     * 保存声音
     *
     * @param trackInfoVo
     * @return
     */
    @GuiGuLogin
    @Operation(summary = "保存声音")
    @PostMapping("/trackInfo/saveTrackInfo")
    public Result saveTrackInfo(@Validated @RequestBody TrackInfoVo trackInfoVo) {
        //1.获取用户ID
        Long userId = AuthContextHolder.getUserId();
        //2.调用业务层保存声音
        trackInfoService.saveTrackInfo(trackInfoVo, userId);
        return Result.ok();
    }


    /**
     * 登录才可以访问
     * 分页条件查询声音列表
     *
     * @param page
     * @param limit
     * @param trackInfoQuery
     * @return
     */
    @GuiGuLogin
    @Operation(summary = "分页条件查询声音列表")
    @PostMapping("/trackInfo/findUserTrackPage/{page}/{limit}")
    public Result<IPage<TrackListVo>> findUserTrackPage(
            @PathVariable Long page,
            @PathVariable Long limit,
            @RequestBody TrackInfoQuery trackInfoQuery) {
        //1.获取当前用户登录ID
        Long userId = AuthContextHolder.getUserId();
        trackInfoQuery.setUserId(userId);
        //2.构建分页对象：封装当前页码、每页记录数
        IPage<TrackListVo> pageInfo = new Page<>(page, limit);
        //3.调用业务层封装分页参数：记录、总记录、总页数
        pageInfo = trackInfoService.findUserTrackPage(pageInfo, trackInfoQuery);
        //4.返回结果
        return Result.ok(pageInfo);
    }


    /**
     * 根据声音ID查询声音信息
     *
     * @param id
     * @return
     */
    @Operation(summary = "根据声音ID查询声音信息")
    @GetMapping("/trackInfo/getTrackInfo/{id}")
    public Result<TrackInfo> getTrackInfo(@PathVariable Long id) {
        TrackInfo trackInfo = trackInfoService.getById(id);
        return Result.ok(trackInfo);
    }

    /**
     * 更新声音信息
     *
     * @param id
     * @param trackInfoVo
     * @return
     */
    @Operation(summary = "更新声音信息")
    @PutMapping("/trackInfo/updateTrackInfo/{id}")
    public Result updateTrackInfo(@PathVariable Long id, @Validated @RequestBody TrackInfoVo trackInfoVo) {
        trackInfoService.updateTrackInfo(trackInfoVo, id);
        return Result.ok();
    }


    /**
     * 删除声音信息
     *
     * @param id
     * @return
     */
    @Operation(summary = "删除声音信息")
    @DeleteMapping("/trackInfo/removeTrackInfo/{id}")
    public Result removeTrackInfo(@PathVariable Long id) {
        trackInfoService.removeTrackInfo(id);
        return Result.ok();
    }


    /**
     * 需求：用户未登录，可以给用户展示声音列表；用户已登录，可以给用户展示声音列表，并动态渲染付费标识
     * 分页查询专辑下声音列表（动态渲染付费标识）
     *
     * @param albumId
     * @param page
     * @param limit
     * @return
     */
    @GuiGuLogin(required = false)
    @Operation(summary = "分页查询专辑下声音列表（动态渲染付费标识）")
    @GetMapping("/trackInfo/findAlbumTrackPage/{albumId}/{page}/{limit}")
    public Result<IPage<AlbumTrackListVo>> findAlbumTrackPage(
            @PathVariable Long albumId,
            @PathVariable Long page,
            @PathVariable Long limit) {
        //1.尝试获取用户ID
        Long userId = AuthContextHolder.getUserId();
        //2.构建分页对象：封装当前页码、每页记录数
        IPage<AlbumTrackListVo> pageInfo = new Page<>(page, limit);

        //3.调用业务逻辑层->持久层：封装：总记录数，总页数，当前页数据
        pageInfo = trackInfoService.findAlbumTrackPage(pageInfo, albumId, userId);

        //4.响应分页对象
        return Result.ok(pageInfo);
    }


    /**
     * 查询声音统计信息
     *
     * @param trackId
     * @return
     */
    @GuiGuLogin(required = false)
    @Operation(summary = "查询声音统计信息")
    @GetMapping("/trackInfo/getTrackStatVo/{trackId}")
    public Result<TrackStatVo> getTrackStatVo(@PathVariable Long trackId) {
        TrackStatVo trackStatVo = trackInfoService.getTrackStatVo(trackId);
        return Result.ok(trackStatVo);
    }


    /**
     * 基于当前用户未购买声音数量动态渲染分集购买列表
     *
     * @param trackId 选择购买声音ID，作为起始标准计算未购买声音数量
     * @return [{name:"本集",price:0.1,trackCount:1},{name:"后10集",price:1,trackCount:10}..,{name:"全集",price:3.8,trackCount:38}]
     */
    @GuiGuLogin
    @Operation(summary = "基于当前用户未购买声音数量动态渲染分集购买列表")
    @GetMapping("/trackInfo/findUserTrackPaidList/{trackId}")
    public Result<List<Map<String, Object>>> findFenjiBuyList(@PathVariable Long trackId) {
        //1.获取当前用户ID
        Long userId = AuthContextHolder.getUserId();
        //2.调用业务逻辑层：封装分集购买列表
        List<Map<String, Object>> list = trackInfoService.findUserTrackPaidList(userId, trackId);
        //3.返回结果
        return Result.ok(list);
    }


    /**
     *  查询用户未购买声音列表
     * @param trackId 选择购买声音ID，作为起始标准计算未购买声音数量
     * @param trackCount 限制查询未购买声音数量
     * @return
     */
    @GuiGuLogin
    @Operation(summary = "查询用户未购买声音列表")
    @GetMapping("/trackInfo/findPaidTrackInfoList/{trackId}/{trackCount}")
    public Result<List<TrackInfo>> findWaitBuyTrackInfoList(@PathVariable Long trackId, @PathVariable Integer trackCount) {
        Long userId = AuthContextHolder.getUserId();
        List<TrackInfo> list = trackInfoService.findWaitBuyTrackInfoList(userId, trackId, trackCount);
        return Result.ok(list);
    }
}

